course_teacher_map = {}

try:
    with open("019.txt", "r", encoding="utf-8") as tfile:
        for line in tfile:
            line = line.strip()  # 使用strip()去除行首行尾的空白字符
            if line:  # 跳过空行
                course, teacher = line.split(",")
                course_teacher_map[course] = teacher.strip()  # 同样去除教师名字两侧的空白字符
    print(course_teacher_map)

    with open("018.txt", "r", encoding="utf-8") as stufile, open("019result.txt", "a+", encoding="utf-8") as refile:
        for line in stufile:
            line = line.strip()
            if line:
                course, sno, name, score = line.split(",")
                teacher = course_teacher_map.get(course.strip())  # 同样去除课程名字两侧的空白字符
                if teacher:
                    result_str = f"{line},{teacher}\n"
                    refile.write(result_str)
                else:
                    print(f"No teacher found for course: {course}")

except FileNotFoundError as e:
    print(f"File not found: {e.filename}")
except Exception as e:
    print(f"An error occurred: {e}")
